package com.dtagri.fjwlw.util;

import android.content.Context;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import com.dtagri.fjwlw.AppContext;
import com.dtagri.fjwlw.base.BaseApplication;

/**
 * @author wxjn
 * 
 */
public class TDevice {
	private static Boolean _isTablet = null;

	public static boolean isTablet() {
		if (_isTablet == null) {
			boolean flag;
			if ((0xf & AppContext.context().getResources().getConfiguration().screenLayout) >= 3)
				flag = true;
			else
				flag = false;
			_isTablet = Boolean.valueOf(flag);
		}
		return _isTablet.booleanValue();
	}

	public static float dpToPixel(float dp) {
		return dp * (getDisplayMetrics().densityDpi / 160F);
	}

	public static DisplayMetrics getDisplayMetrics() {
		DisplayMetrics displaymetrics = new DisplayMetrics();
		((WindowManager) AppContext.context().getSystemService(
				Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(
				displaymetrics);
		return displaymetrics;
	}

	public static float getScreenWidth() {
		return getDisplayMetrics().widthPixels;
	}

	public static void hideSoftKeyboard(View view) {
		if (view == null)
			return;
		((InputMethodManager) AppContext.context().getSystemService(
				Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
				view.getWindowToken(), 0);
	}

	public static boolean hasInternet() {
		boolean flag;
		if (((ConnectivityManager) AppContext.context().getSystemService(
				"connectivity")).getActiveNetworkInfo() != null)
			flag = true;
		else
			flag = false;
		return flag;
	}

	public static int getVersionCode() {
		int versionCode = 0;
		try {
			versionCode = BaseApplication
					.context()
					.getPackageManager()
					.getPackageInfo(BaseApplication.context().getPackageName(),
							0).versionCode;
		} catch (PackageManager.NameNotFoundException ex) {
			versionCode = 0;
		}
		return versionCode;
	}

	public static String getVersionName() {
		String name = "";
		try {
			name = BaseApplication
					.context()
					.getPackageManager()
					.getPackageInfo(BaseApplication.context().getPackageName(),
							0).versionName;
		} catch (PackageManager.NameNotFoundException ex) {
			name = "";
		}
		return name;
	}
}
